package cn.afactory.tygz.ui.base.views.search;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.adapter.BaseListAdapter;
import cn.afactory.tygz.ui.base.entity.LoganThinkWordEntity;

public class SearchAlertView extends LinearLayout {

    private ListView search_alert_lv;
    private SearchAlertAdapter mAdapter;
    private OnItemClickListener mListener;

    public SearchAlertView(Context context) {
        super(context);
        initView();
    }

    public SearchAlertView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public SearchAlertView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        View view = inflate(getContext(), R.layout.search_alert_view, this);
        search_alert_lv = (ListView) view.findViewById(R.id.search_alert_lv);
        mAdapter = new SearchAlertAdapter(getContext());
        search_alert_lv.setAdapter(mAdapter);
        search_alert_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListener != null) {
                    mListener.OnItemClick(view, position, mAdapter.getItem(position));
                }
            }
        });
    }

    /**
     * @param data
     */
    public void setData(List<LoganThinkWordEntity> data) {
        mAdapter.cleanAll();
        if (data != null) {
            mAdapter.addDataAll(data);
        }
        mAdapter.notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        mListener = l;
    }

    /**
     *
     */
    class SearchAlertAdapter extends BaseListAdapter<LoganThinkWordEntity, SearchAlertAdapter.AlertViewHolder> {

        public SearchAlertAdapter(Context ctx) {
            super(ctx);
        }

        @Override
        public View newView(int position, LayoutInflater inflater, ViewGroup parent) {
            View view = View.inflate(mContext, R.layout.search_alert_item_layout, null);
            return view;
        }

        @Override
        public AlertViewHolder onViewHolderCreate(int position, View view) {
            AlertViewHolder hvh = new AlertViewHolder(view);
            return hvh;
        }

        @Override
        public void onBindViewHolder(int position, LoganThinkWordEntity itemData, AlertViewHolder viewHolder) {
            viewHolder.search_alert_keyword.setText(itemData.content);
        }

        class AlertViewHolder extends BaseListAdapter.ViewHolder {
            TextView search_alert_keyword;

            AlertViewHolder(View view) {
                search_alert_keyword = (TextView) view.findViewById(R.id.search_alert_keyword);
            }
        }
    }

    public interface OnItemClickListener {
        void OnItemClick(View v, int position, LoganThinkWordEntity itemDate);
    }


}
